package com.lin.manager.common.resp;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Data;

import java.util.List;

/**
 * @author linjiang
 * @date 2024/7/9
 * @fn
 */
@Data
public class DataPage<T> {
    /**
     * 分页页码
     */
    private Long pageNum;
    /**
     * 分页大小
     */
    private Long pageSize;
    /**
     * 分页数据
     */
    private List<T> data;
    /**
     * 分页总数
     */
    private Long total;

    public static <T> DataPageBuilder<T> builder() {
        return new DataPageBuilder<>();
    }

    public static class DataPageBuilder<T> {
        private Long pageNum;
        private Long pageSize;
        private List<T> data;
        private Long total;

        public DataPageBuilder<T> pageNum(long pageNum) {
            this.pageNum = pageNum;
            return this;
        }

        public DataPageBuilder<T> pageSize(long pageSize) {
            this.pageSize = pageSize;
            return this;
        }

        public DataPageBuilder<T> total(Long total) {
            this.total = total;
            return this;
        }

        public DataPageBuilder<T> data(List<T> data) {
            this.data = data;
            return this;
        }

        public DataPageBuilder<T> page(Page<T> page) {
            this.pageNum = page.getCurrent();
            this.pageSize = page.getSize();
            this.total = page.getTotal();
            this.data = page.getRecords();
            return this;
        }

        public DataPage<T> build() {
            DataPage<T> dataPage = new DataPage<>();
            dataPage.setData(data);
            dataPage.setPageNum(pageNum);
            dataPage.setPageSize(pageSize);
            dataPage.setTotal(total);
            return dataPage;
        }
    }
}
